﻿using System.Collections.Generic;
using System.Linq;
using DMS.DAOs;
using DMS.Filters;
using DMS.BEs;
using DMS.DTOs;

namespace DMS.Services.Impl
{
	public class InvoiceService : ServiceBase, IInvoiceService
	{
		private readonly IInvoiceDAO invoiceDAO;

		public InvoiceService(IInvoiceDAO invoiceDAO)
		{
			this.invoiceDAO = invoiceDAO;
		}

		public ResultDTO<InvoiceWithLinesDTO> GetInvoiceById(int invoiceId)
		{
			Invoice invoice = invoiceDAO.GetById(invoiceId);

			if (invoice == null)
				return new ResultDTO<InvoiceWithLinesDTO>(null) { Messages = new[] { "invoice not found" } };


			InvoiceWithLinesDTO dto = mapperBEtoDTO.Map<Invoice, InvoiceWithLinesDTO>(invoice);
			return new ResultDTO<InvoiceWithLinesDTO>(dto);
		}

		public ResultListDTO<InvoiceDTO> GetInvoicesByContry(string country)
		{
			IList<Invoice> invoices = invoiceDAO.GetByFilter(new InvoiceFilter() { Country = country });

			if (invoices.Count == 0)
				return new ResultListDTO<InvoiceDTO>(null) { Messages = new[] { "invoices not found" } };

			IEnumerable<InvoiceDTO> items = (from i in invoices select mapperBEtoDTO.Map<Invoice, InvoiceDTO>(i));
			return new ResultListDTO<InvoiceDTO>(items.ToArray());
		}
	}
}
